package com.jewelry.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jewelry.common.util.Constants;
import com.jewelry.common.util.SequenceUtil;
import com.jewelry.dao.ISeqDAO;
import com.jewelry.service.ISeqService;

public class SeqServiceImpl implements ISeqService {

	private static Logger logger = LoggerFactory
			.getLogger(SeqServiceImpl.class);

	private ISeqDAO seqDAO;

	public ISeqDAO getSeqDAO() {
		return seqDAO;
	}

	public void setSeqDAO(ISeqDAO seqDAO) {
		this.seqDAO = seqDAO;
	}

	// ==============================================

	@Override
	public synchronized String getOrderID() {
		if (logger.isDebugEnabled()) {
			logger.debug("start getOrderID ... ");
		}

		int orderID = seqDAO.getNowOrderSeq();
		// 重置
		if (orderID == Constants.SEQ_INDEX_MAX) {
			seqDAO.resetOrderSeq();// 从1开始
		} else {
			seqDAO.updateOrderSeq();
		}
		return SequenceUtil.getSeq(orderID);
	}

	@Override
	public synchronized String getCustomerID() {
		int customerID = seqDAO.getNowCustomerSeq();
		// 重置
		if (customerID == Constants.SEQ_INDEX_MAX) {
			seqDAO.resetCustomerSeq();// 从1开始
		} else {
			seqDAO.updateCustomerSeq();
		}
		return SequenceUtil.getSeq(customerID);
	}

	@Override
	public String getMaterialStoneID() {
		int materialStone = seqDAO.getNowMaterialStoneSeq();
		// 重置
		if (materialStone == Constants.SEQ_INDEX_MAX) {
			seqDAO.resetMaterialStoneSeq();// 从1开始
		} else {
			seqDAO.updateMaterialStoneSeq();
		}
		return SequenceUtil.getSeq(materialStone);
	}

}
